package com.commands
{
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.commands.Command;
	import com.events.LoginEvent;
	import mx.controls.Alert;
	import com.model.ModelLocator;
	import flash.events.Event;
	import flash.html.HTMLControl;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import com.components.gestorQuiniela;
	import com.datos.Datos;
	import com.datos.ItemEscrutinio;
	import mx.core.Application;
	import com.events.CargarEvent;

	public class CargarCommand implements Command
	{
		private var modelLocator : ModelLocator = ModelLocator.getInstance();
		
		public function execute(event:CairngormEvent):void
		{
	        var cargarEvent:CargarEvent = CargarEvent(event);  
			cargar();
		}

        private function cargar():void
        {
			//Alert.show("Entra en cargar");         	
			var control : flash.html.HTMLControl = new HTMLControl();
			var url_quiniela : flash.net.URLRequest  = new URLRequest(modelLocator.link);         	
			var ldr : flash.net.URLLoader = new URLLoader ();
			ldr.load(url_quiniela);
			ldr.addEventListener(Event.COMPLETE,onLoad);  
      	
        }

		private function onLoad(event:Event):void
		{
			//Alert.show("Entra en onload");       				
			var ristra : String = event.target.data;
			var hasta : int = ristra.indexOf("<!DOCTYPE",0);
			var result : String = ristra.substring(0,hasta);
			modelLocator.html = result;
			parsear();
		}        
        
		private function parsear():void
		{
			//Alert.show("Entra en parsear"); 			
		 	var array : Array = modelLocator.html.split(";",modelLocator.html.length);
		 	var arrayDate : Array = String(array[3]).substring(0,10).split("/",String(array[3]).substring(0,10).length);
		 	modelLocator.fechaDate = new Date(arrayDate[2],--arrayDate[1],arrayDate[0]);
		 	modelLocator.selectedDate = modelLocator.fechaDate;
		 	modelLocator.fecha = /*Jornada */array[2] + " a día " + String(array[3]).substring(0,10);
		 	
		 	var num:int=1;
		 	var i:int=4;
		 	var limit:int = 34;
		 	var posQuinielaGanadora :int = 34;         	
		 	//borramos el array de datos
		 	modelLocator.arrayDatos.removeAll();
		 	
		 	//Parseo de encuentros
		 	for (i = 4; i<limit;i++)
		 	{
		 		var datos:Datos = new Datos();	
		 		var myPattern:RegExp = /<-193->/gi;   //á
		 		var myPattern2:RegExp = /<-201->/gi;  //é
		 		var myPattern3:RegExp = /<-211->/gi;  //ó
		 		var myPattern4:RegExp = /<-205->/gi;  //í        		
		 		var myPattern5:RegExp = /<-218->/gi;  //ú       
		 		var myPattern6:RegExp = /<-241->/gi;  //ñ   
		 		var myPattern7:RegExp = /<-209->/gi;  //Ñ     		
		 		array[i] = String(array[i]).replace(myPattern,"Á");
		 		array[i] = String(array[i]).replace(myPattern2,"É");
		 		array[i] = String(array[i]).replace(myPattern3,"Ó");         		
		 		array[i] = String(array[i]).replace(myPattern4,"Í"); 
		 		array[i] = String(array[i]).replace(myPattern5,"Ú"); 
		 		array[i] = String(array[i]).replace(myPattern6,"ñ"); 
		 		array[i] = String(array[i]).replace(myPattern7,"Ñ");
		 		datos.local=array[i];
		 		i++;
		 		array[i] = String(array[i]).replace(myPattern,"Á");
		 		array[i] = String(array[i]).replace(myPattern2,"É");
		 		array[i] = String(array[i]).replace(myPattern3,"Ó");
		 		array[i] = String(array[i]).replace(myPattern4,"Í");  
		 		array[i] = String(array[i]).replace(myPattern5,"Ú"); 
		 		array[i] = String(array[i]).replace(myPattern6,"ñ"); 
		 		array[i] = String(array[i]).replace(myPattern7,"Ñ");         		
		 		datos.visitante=array[i];
		 		datos.encuentro = datos.local + " - " + datos.visitante;
		 		//Parseo de Numeracion
		 		datos.numero = num++;
		 		//Parseo de apuesta ganadora
		 		datos.ganadora = array[posQuinielaGanadora++]; 
		 		
		 		modelLocator.arrayDatos.addItem(datos);    		
		 	} 
		 	
		 	//Parseo de escrutinio
		 	modelLocator.arrayEscrutinio.removeAll();
		 	var numeroApuestasGanadoras : uint = 15;
		 	var posEscrutinio :int = posQuinielaGanadora;
			for (var j:uint=0;j<6;j++)
			{         		
		 		var escrutinio:ItemEscrutinio = new ItemEscrutinio();
		 		escrutinio.numeroAcierto = array[posEscrutinio]; 
		 		escrutinio.precioAcierto = array[posEscrutinio+6]; 
		 		posEscrutinio++;
		 		escrutinio.numero = numeroApuestasGanadoras--;         		        
		     	modelLocator.arrayEscrutinio.addItem(escrutinio);  
			}	                    	
		
		}        		
		
	}
}